﻿///    /************************************************************************ 
///     * uoLib library for .Net projects.
///     * Copyright (c) 2009 by uonun
///     * Homepage: http://uolib.ourome.com
///     * 
///     * This program is free software: you can redistribute it and/or modify
///     * it under the terms of the GNU General Public License as published by
///     * the Free Software Foundation, either version 3 of the License, or
///     * (at your option) any later version.

///     * This program is distributed in the hope that it will be useful,
///     * but WITHOUT ANY WARRANTY; without even the implied warranty of
///     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
///     * GNU General Public License for more details.

///     * You should have received a copy of the GNU General Public License
///     * along with this program.  If not, see http://www.gnu.org/licenses/gpl.html.
///     ***********************************************************************/ 

using System;
using System.IO;
using System.Threading;
using System.Windows.Forms;

namespace uoLib.Win.Debugger
{
    /// <summary>
    /// 提供<see cref="uoLib.AvailableTypeAttribute.Type.WinForm"/>程序在运行时的日志、异常记录。
    /// </summary>
    /// <example>
    /// 此类常用于<see cref="uoLib.AvailableTypeAttribute.Type.WinForm"/>程序在运行过程中的日志记录。如：
    /// <code><![CDATA[
    /// static class Program
    /// {
    ///     static void Main(string[] args)
    ///     {
    ///         Loger.Write("主程序开始执行：");
    ///         Loger.Write("wait for 5 seconds...");
    ///         Thread.Sleep(5000);
    ///         //do something...
    ///         Loger.Write("doing something...");
    ///         Loger.Write("wait for 5 seconds...");
    ///         Thread.Sleep(5000);
    ///         //do other things...
    ///         Loger.Write("doing other things...");
    ///         
    ///         Loger.Write("主程序结束。");
    ///     }
    /// }]]></code>
    /// 此段代码将输出一下 log 文件：
    /// <code><![CDATA[
    /// [2009-4-21 17:12:02] 主程序开始执行：
    /// [2009-4-21 17:12:02] wait for 5 seconds...
    /// [2009-4-21 17:12:07] doing something...
    /// [2009-4-21 17:12:07] wait for 5 seconds...
    /// [2009-4-21 17:12:12] doing other things...
    /// [2009-4-21 17:12:12] 主程序结束。
    /// ]]></code>
    /// </example>
    public static class Logger
    {
        /// <summary>
        /// 将日志信息写入到Logs文件夹中。
        /// </summary>
        /// <param name="msg"></param>
        public static void Write(string msg)
        {
            WriteLogProvider(msg, "Logs");
        }

        /// <summary>
        /// 将异常信息写入到Errors文件夹中。
        /// </summary>
        /// <param name="ex"></param>
        public static void Write(Exception ex)
        {
            WriteLogProvider(ex.ToString(), "Errors");
            WriteLogProvider("===========================================================", "Errors");
            WriteLogProvider(Environment.NewLine, "Logs");
        }

        private static void WriteLogProvider(string msg, string folderName)
        {
            try
            {
                FileInfo fi = new FileInfo(Application.ExecutablePath);
                string logFolder = string.Format(@"{0}\{1}", fi.Directory.FullName, folderName);
                DirectoryInfo di = new DirectoryInfo(logFolder);
                if (!di.Exists) di.Create();

                StreamWriter sw = new StreamWriter(string.Format(@"{0}\{1:yyyyMMdd}.log", di.FullName, DateTime.Now), true);
                if (msg == Environment.NewLine)
                {
                    sw.WriteLine(Environment.NewLine);
                }
                else
                {
                    sw.WriteLine(string.Format("[{0}] {1}", DateTime.Now, msg));
                }
                sw.Close();
            }
            catch { }
        }
    }
}

